﻿using System.ComponentModel;
using System;
using System.Xml.Serialization;
using Gildor.SimpleHostMonitor.Desktop.Entities;
using System.Diagnostics;

namespace Gildor.SimpleHostMonitor.Desktop.Entities
{
    /// <summary>
    /// The information of a host.
    /// </summary>
    [Serializable]
    [DebuggerDisplay ("Address = {Address}")]
    public class HostInfo : INotifyPropertyChanged
    {
        private string _address;

        /// <summary>
        /// Gets or sets the address.
        /// </summary>
        /// <value>The address.</value>
        [XmlAttribute]
        public string Address
        {
            get { return _address; }
            set
            {
                if (value != _address)
                {
                    _address = value;
                    PropertyChanged (this, new PropertyChangedEventArgs ("Address"));
                }
            }
        }

        private string _state = "Unknown";

        /// <summary>
        /// Gets or sets the state.
        /// </summary>
        /// <value>The state.</value>
        [XmlIgnore]
        public string State
        {
            get { return _state; }
            set
            {
                if (value != _state)
                {
                    _state = value;
                    PropertyChanged (this, new PropertyChangedEventArgs ("State"));
                }
            }
        }

        /// <summary>
        /// Gets or sets the failure count.
        /// </summary>
        /// <value>The failure count.</value>
        [XmlIgnore]
        public int FailureCount { get; set; }

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        /// <summary>
        /// Strategies of notify.
        /// </summary>
        public enum NotifyStrategies
        {
            /// <summary>
            /// Notify when failed.
            /// </summary>
            Fail,
            /// <summary>
            /// Notify when succeed.
            /// </summary>
            Success,
            /// <summary>
            /// Never notify.
            /// </summary>
            Never
        }

        private NotifyStrategies _notifyStrategy = NotifyStrategies.Fail;

        /// <summary>
        /// Gets or sets the notify strategy.
        /// </summary>
        /// <value>The notify strategy.</value>
        [XmlAttribute]
        public NotifyStrategies NotifyStrategy
        {
            get { return _notifyStrategy; }
            set
            {
                if (value != _notifyStrategy)
                {
                    _notifyStrategy = value;
                    PropertyChanged (this, new PropertyChangedEventArgs ("NotifyStrategy"));
                }
            }
        }

        #region [New properties in version 0.2]

        private int _failureThreshold;

        /// <summary>
        /// Gets or sets the failure threshold to notify.
        /// </summary>
        /// <value>The failure threshold.</value>
        [XmlAttribute]
        public int FailureThreshold
        {
            get { return _failureThreshold; }
            set
            {
                if (value != _failureThreshold)
                {
                    _failureThreshold = value;
                    PropertyChanged (this, new PropertyChangedEventArgs ("FailureThreshold"));
                }
            }
        }

        [XmlIgnore]
        public bool FailureThresholdSpecified { get; set; }

        [XmlIgnore]
        public bool NotifyStrategySpecified { get; set; }

        private string _description;

        /// <summary>
        /// Gets or sets the description of the host.
        /// </summary>
        /// <value>The description.</value>
        [XmlElement]
        public string Description
        {
            get { return _description; }
            set
            {
                if (value != _description)
                {
                    _description = value;
                    PropertyChanged (this, new PropertyChangedEventArgs ("Description"));
                }
            }
        }

        [XmlIgnore]
        public MonitorLog Log { get; set; }

        #endregion

        public HostInfo ()
        {
            Log = new MonitorLog ();
        }
    }
}
